import { render } from 'vue';

import { downloadSource } from './download-source';

//表格导出 传入ElTable生成的vnode
export const exportTableByVNode = async (vnode: VNode, name: string) => {
  const div = document.createElement('div');
  render(vnode, div);
  await nextTick();
  await nextTick();
  div.querySelectorAll('td,th').forEach((el) => {
    el.setAttribute('style', 'border:0.5px #aaa solid;text-align:center');
    el.querySelectorAll('div').forEach((el) => {
      el.setAttribute('style', 'text-align:center');
    });
  });

  let blob = new Blob([div.outerHTML], { type: 'text/plain;charset=utf-8' }); //解决中文乱码问题
  blob = new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type });
  div.remove();
  downloadSource(blob, name);
};
